package com.tamecode.chapter06.sec3;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * ①继承PropertyPlaceholderConfigurer定义支持密文版属性的属性配置器
 * @Author: Qiongchao
 * @Version: 1.0.0
 * @Date: 2023/9/25
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private static final Set<String> ENCRYPT_KEYS = new HashSet<>(Arrays.asList("username", "password"));

    /**
     * ②对特定属性的属性值进行转换
     * @param propertyName
     * @param propertyValue
     * @return
     */
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (isEncryptedProperty(propertyName)) {
            String decryptString = DESUtils.getDecryptString(propertyValue);
            System.out.println("DES 解析出来：" + decryptString);
            return decryptString;
        }
        return super.convertProperty(propertyName, propertyValue);
    }

    private boolean isEncryptedProperty(String propertyName) {
        return ENCRYPT_KEYS.contains(propertyName);
    }
}
